package com.guiji.template.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.guiji.common.utils.PageUtils;
import com.guiji.common.core.page.PagePlus;
import com.guiji.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.guiji.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guiji.template.bo.TemplateFieldAddBo;
import com.guiji.template.bo.TemplateFieldQueryBo;
import com.guiji.template.bo.TemplateFieldEditBo;
import com.guiji.template.domain.TemplateField;
import com.guiji.template.mapper.TemplateFieldMapper;
import com.guiji.template.vo.TemplateFieldVo;
import com.guiji.template.service.ITemplateFieldService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 模板字段Service业务层处理
 *
 * @author 钟维澎
 * @date 2021-07-01
 */
@Service
public class TemplateFieldServiceImpl extends ServicePlusImpl<TemplateFieldMapper, TemplateField> implements ITemplateFieldService {

	@Override
	public TemplateFieldVo queryById(Long id){
		return getVoById(id, TemplateFieldVo.class);
	}

	@Override
	public TableDataInfo<TemplateFieldVo> queryPageList(TemplateFieldQueryBo bo) {
		PagePlus<TemplateField, TemplateFieldVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), TemplateFieldVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<TemplateFieldVo> queryList(TemplateFieldQueryBo bo) {
		return listVo(buildQueryWrapper(bo), TemplateFieldVo.class);
	}

	private LambdaQueryWrapper<TemplateField> buildQueryWrapper(TemplateFieldQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<TemplateField> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getId() != null, TemplateField::getId, bo.getId());
		lqw.eq(bo.getTemplateId() != null, TemplateField::getTemplateId, bo.getTemplateId());
		lqw.like(StrUtil.isNotBlank(bo.getFieldName()), TemplateField::getFieldName, bo.getFieldName());
		lqw.eq(bo.getFieldType() != null, TemplateField::getFieldType, bo.getFieldType());
		lqw.eq(StrUtil.isNotBlank(bo.getDefaultFieldValue()), TemplateField::getDefaultFieldValue, bo.getDefaultFieldValue());
		lqw.eq(bo.getVersion() != null, TemplateField::getVersion, bo.getVersion());
		lqw.eq(bo.getCreateTime() != null, TemplateField::getCreateTime, bo.getCreateTime());
		lqw.eq(StrUtil.isNotBlank(bo.getCreateBy()), TemplateField::getCreateBy, bo.getCreateBy());
		lqw.eq(bo.getUpdateTime() != null, TemplateField::getUpdateTime, bo.getUpdateTime());
		lqw.eq(StrUtil.isNotBlank(bo.getUpdateBy()), TemplateField::getUpdateBy, bo.getUpdateBy());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(TemplateFieldAddBo bo) {
		TemplateField add = BeanUtil.toBean(bo, TemplateField.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(TemplateFieldEditBo bo) {
		TemplateField update = BeanUtil.toBean(bo, TemplateField.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(TemplateField entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
